package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.Category;
import org.devocean.groupbuying.service.CategoryService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/gb-cpanel/categories" })
public class CategoriesController {
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping(value = "/")
	public Map<String, ? extends Object> list(
			@RequestParam(value = "query",defaultValue="") String query,
			@RequestParam(value = "start", required = false,defaultValue="0") Integer start,
			@RequestParam(value = "limit", required = false,defaultValue="0") Integer size) {
		try {
				List<Category> categories = categoryService.findCategoriesByCriteria(query, start, size);
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				for (Category category : categories) {
						Map<String, Object> data = new ConcurrentHashMap<String, Object>();
						data.put("id", category.getCategoryID());
						data.put("CL_id", category.getCategoriesLocalization().getCategoriesLocalizationID());
						data.put("name", category.getName());
						data.put("lang", category.getLang()==null ?"":category.getLang() );
						data.put("model", category.getModel()==null ?"":category.getModel());
						result.add(data);
				}
				Long total = categoryService.countCategoriesByCriteria(query);
				return RenderMapUtils.getMap(result,total);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving Categories from database.");
		}
	}
		
	@RequestMapping(value = "/tree")
	public Map<String, ? extends Object> tree(@RequestParam(value = "node", required = false) String node) {
		try {
			
			if(node != null){
			if (node.equals("root")) {	
				List<Category> categories = new ArrayList<Category>();
				categories = categoryService.findAllCategorys();
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Category category : categories) {
					if (category.getCategory() == null) {
						Map<String, Object> data = new ConcurrentHashMap<String, Object>();
						data.put("id", category.getCategoryID());
						data.put("name", category.getName());
						data.put("lang", category.getLang()==null ?"":category.getLang() );
						data.put("model", category.getModel()==null ?"":category.getModel());
						data.put("CL_id", category.getCategoriesLocalization().getCategoriesLocalizationID());
						data.put("level", i);
						if (category.getCategories().size() > 0)
							data.put("leaf", false);
						else
							data.put("leaf", true);

						result.add(data);
					}
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			} else {
				Category root = categoryService.findCategoryById(Integer.parseInt(node));
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Category category : root.getCategories()) {
					Map<String, Object> data = new ConcurrentHashMap<String, Object>();
					data.put("id", category.getCategoryID());
					data.put("name", category.getName());
					data.put("lang", category.getLang());
					data.put("model", category.getModel());
					data.put("CL_id", category.getCategoriesLocalization().getCategoriesLocalizationID());
					data.put("level", i);
					if (category.getCategories().size() > 0)
						data.put("leaf", false);
					else
						data.put("leaf", true);

					result.add(data);
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			}
			}
			else{
				return RenderMapUtils.getModelMapError("Error retrieving Categories from database.");
			}

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving Categories from database.");
		}
	}
	
	
	@RequestMapping(value = "/{id}/category/{category_id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable("id") Integer id,@PathVariable("category_id") Integer categoryId) {
		categoryService.deleteCategory(id, categoryId);
	}


}
